package com.cicadalane.androlate;

/*
 * Copyright (C) 2011 cicada.software@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

class ReviewPage extends AndrolateWizardPage implements Listener {

	public static final int TRANSLATEREVIEW_TABLE_FILENAME_INDEX = 1;
	public static final int TRANSLATEREVIEW_TABLE_LANGUAGE_INDEX = 0;
	public static final int TRANSLATEREVIEW_TABLE_NAME_INDEX = 2;
	public static final String[] TRANSLATEREVIEW_TABLE_TITLES = new String[] {
			"Language", "Filename", "Name", "Value" };
	public static final int TRANSLATEREVIEW_TABLE_VALUE_INDEX = 3;

	private Button mBackupFiles = null;

	private Button mOverwriteButton = null;

	private Button mResubmitUnchangedButton = null;

	private Button mLogDetailsButton = null;
	
	private Button mRememberSettings = null;

	private Table mTranslateTable = null;

	private Label mTranslationSummary = null;
	
	private Label mTranslationEmpty = null;

	protected ReviewPage(String pageName) {
		super(pageName);
		setTitle(pageName);
		setDescription("Review your selection below.  Click back to make changes, or finish to begin translating");
	}

	@Override
	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);

		GridLayout gl = new GridLayout();
		composite.setLayoutData(new GridData(GridData.FILL));
		gl.numColumns = 2;
		gl.makeColumnsEqualWidth = true;
		composite.setLayout(gl);

		Group groupBox = new Group(composite, SWT.SHADOW_ETCHED_IN);
		groupBox.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		((GridData) groupBox.getLayoutData()).horizontalSpan = 2;
		groupBox.setText("Options:");
		FillLayout rowLayout = new FillLayout();
		rowLayout.type = SWT.VERTICAL;
		groupBox.setLayout(new GridLayout());

		mOverwriteButton = new Button(groupBox, SWT.CHECK | SWT.BOLD);
		mOverwriteButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		((GridData) mOverwriteButton.getLayoutData()).horizontalSpan = 2;
		mOverwriteButton.setText("Overwrite existing values");
		mOverwriteButton.addListener(SWT.Selection, this);

		mRememberSettings = new Button(groupBox, SWT.CHECK);
		mRememberSettings.setLayoutData(new GridData(GridData.FILL));
		((GridData) mRememberSettings.getLayoutData()).horizontalSpan = 2;
		mRememberSettings.setText("Remember settings for next time");
		mRememberSettings.setSelection(true);
		getAndrolateWizard().setRememberSettingsOption(true);
		mRememberSettings.addListener(SWT.Selection, this);

		mBackupFiles = new Button(groupBox, SWT.CHECK);
		mBackupFiles.setLayoutData(new GridData(GridData.FILL));
		((GridData) mBackupFiles.getLayoutData()).horizontalSpan = 2;
		mBackupFiles.setText("Backup files before modifying");
		String backupStr = getAndrolate().getOption(
				Androlate.OPTION_KEY_BACKUP_FILES);
		boolean backup = false;
		backup = Boolean.parseBoolean(backupStr);
		mBackupFiles.setSelection(backup);
		mBackupFiles.addListener(SWT.Selection, this);

		mResubmitUnchangedButton = new Button(groupBox, SWT.CHECK | SWT.BOLD);
		mResubmitUnchangedButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		((GridData) mResubmitUnchangedButton.getLayoutData()).horizontalSpan = 2;
		mResubmitUnchangedButton.setText("Resubmit values that haven't changed");
		mResubmitUnchangedButton.addListener(SWT.Selection, this);

		mLogDetailsButton = new Button(groupBox, SWT.CHECK | SWT.BOLD);
		mLogDetailsButton.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		((GridData) mLogDetailsButton.getLayoutData()).horizontalSpan = 2;
		mLogDetailsButton.setSelection(getAndrolateWizard().getUseEclipseLog());
		mLogDetailsButton.setText("Log progress to Eclipse log");
		mLogDetailsButton.addListener(SWT.Selection, this);
		
		mTranslationSummary = new Label(composite, SWT.NONE);
		mTranslationSummary.setLayoutData(new GridData(GridData.FILL));
		((GridData) mTranslationSummary.getLayoutData()).horizontalSpan = 2;
		mTranslationSummary.setText("Translation Summary:");

		mTranslationEmpty = new Label(composite, SWT.NONE);
		mTranslationEmpty.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		((GridData) mTranslationEmpty.getLayoutData()).horizontalSpan = 2;
		mTranslationEmpty.setText("No strings found for translation, try checking \"Overwrite exiting values\".");
		mTranslationEmpty.setBackground(SwtUtils.convertAwtColorToSwtColor(composite, java.awt.Color.YELLOW));
		mTranslationEmpty.setVisible(false);

		
		mTranslateTable = createTranslateTable(composite);

		setControl(composite);
	}

	private Table createTranslateTable(final Composite composite) {
		Table table = new Table(composite, SWT.MULTI | SWT.V_SCROLL
				| SWT.H_SCROLL);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		table.setLayoutData(new GridData(GridData.FILL_BOTH));
		((GridData) table.getLayoutData()).horizontalSpan = 2;

		for (int i = 0; i < TRANSLATEREVIEW_TABLE_TITLES.length; i++) {
			TableColumn column = new TableColumn(table, SWT.NONE);
			column.setText(TRANSLATEREVIEW_TABLE_TITLES[i]);
		}
		return table;
	}

	@Override
	public void handleEvent(Event event) {
		if (event.widget == mRememberSettings) {
			getAndrolateWizard().setRememberSettingsOption(
					mRememberSettings.getSelection());
			return;
		}
		if (event.widget == mBackupFiles) {
			getAndrolate().setOption(Androlate.OPTION_KEY_BACKUP_FILES,
					mBackupFiles.getSelection());
			return;
		}
		if (event.widget == mOverwriteButton) {
			getAndrolate().setOption(Androlate.OPTION_KEY_OVERWRITE_EXISTING,
					mOverwriteButton.getSelection());
		}
		if (event.widget == mResubmitUnchangedButton) {
			getAndrolate().setOption(Androlate.OPTION_KEY_SUBMIT_UNCHANGED,
					mResubmitUnchangedButton.getSelection());
		}
		if (event.widget == mLogDetailsButton) {
			getAndrolateWizard().setUseEclipseLog(mLogDetailsButton.getSelection());		
		}
		setPageComplete(validatePage());
	}

	@Override
	public void onEnterPage() {
		super.onEnterPage();
		setPageComplete(validatePage());
	}

	private boolean validatePage() {
		boolean result = false;
		mTranslateTable.removeAll();
		if (getAndrolate() == null)
			return false;

		List<AndrolateLanguage> languages = getAndrolate().getTargetLanguages();
		List<AndrolateOperation> operations = getAndrolate().getOperations();
		for (AndrolateLanguage l : languages) {
			for (AndrolateOperation operation : operations) {
				ArrayList<StringData> resolvedStringList = getAndrolateApplication()
						.resolveStringOperations(operation, l);
				if (resolvedStringList.size() > 0) {
					mTranslationEmpty.setVisible(false);
					mTranslateTable.setVisible(true);
					result = true;
					
					for (StringData s : resolvedStringList) {
						String name = s.getName();
						if (name == null || name.isEmpty())
							continue;

						String filenameDisplay = operation.getFilename();
						java.io.File projectFilePath = getAndrolateWizard()
								.getProjectPath().toFile();
						filenameDisplay = AndrolateApplication
								.stringIntersection(filenameDisplay,
										projectFilePath.getAbsolutePath());

						TableItem newTranslateTableItem = new TableItem(
								mTranslateTable, SWT.NONE);
						newTranslateTableItem.setText(
								TRANSLATEREVIEW_TABLE_LANGUAGE_INDEX, l
										.getDisplay()

						);
						newTranslateTableItem.setText(
								TRANSLATEREVIEW_TABLE_FILENAME_INDEX,
								filenameDisplay

						);
						newTranslateTableItem.setText(
								TRANSLATEREVIEW_TABLE_NAME_INDEX, name);
						newTranslateTableItem
								.setText(TRANSLATEREVIEW_TABLE_VALUE_INDEX, s
										.getValue());
						newTranslateTableItem.setData(s);
					}
				}
				else {
					mTranslationEmpty.setVisible(true);
					mTranslateTable.setVisible(false);
					result = false;
				}
			}
		}

		for (int i = 0; i < mTranslateTable.getColumnCount(); i++) {
			mTranslateTable.getColumn(i).pack();
		}
		return result;
	}
}
